﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Security.Permissions;
using System.Web.Services.Description;
using System.Collections.Generic;
using System.Web.Configuration;
public partial class Register : Basepage
{
    //khoi tao ket noi csdl
    static string strconn = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    SqlConnection con = new SqlConnection(strconn);
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void CreateButton_Click(object sender, EventArgs e)
    {
        string url = this.Page.Request.Url.AbsoluteUri;//get the url of current page

        //kiem tra ten dang nhap da ton tai
        if(isExistUser(UserName.Text.Trim()))
        {
            if(url.Contains("lang=vi"))
            {
                ErrorMessage.Text = "Tên đăng nhập đã tồn tại";
            }
            else
            {
                ErrorMessage.Text =" Username has already exist";
            }
            return;
        }

        //insert account neu thanh cong

         try
        {
           
            con.Open();  
            
            string insert = "INSERT INTO ThanhVien VALUES (N'" + UserName.Text.Trim()
                                                        + "', N'" + Password.Text.Trim()
                                                        + "', N'" + UserType.SelectedValue.Trim()
                                                        + "', N'" + FullName.Text.Trim()
                                                        + "', N'" + Email.Text.Trim()
                                                        + "', N'" + PhoneNumber.Text.Trim()
                                                        + "', N'" + CardID.Text.Trim()
                                                        + "')";
            SqlCommand create = new SqlCommand(insert, con);
            create.ExecuteNonQuery();
        }
        catch (Exception)
        {
            return;
        }
         //sendMail(Email.Text.Trim());
         Response.Redirect("CompleteSignUp.aspx");
        
       
    }
    //kiem tra user hop le
    public bool isExistUser(string username)
    {
        con.Open();
        SqlCommand usercheck = new SqlCommand("select * from ThanhVien where TenDangNhap='"+username.Trim()+"'", con);
        SqlDataReader reader = usercheck.ExecuteReader();

        if (reader.HasRows)
        {
            con.Close();
            return true;
        }
        con.Close();
        return false;

    }
    //send mail to new account
    public void sendMail(string emailAddress)
    {
        // Gui mail chua thong tin tai khoan
        string content = "Your new account in <a href=\"http://moigioinama.somee.com\">Nam A Website</a>:"
            + "<ul><li>Username: <strong>" + UserName.Text.Trim()
            + "</strong></li><li>Password: <strong>" + Password.Text.Trim()
            + "</strong></li><li>Account Type: <strong>" + UserType.Text.Trim()
            + "</strong></li></ul>"
            + "<br /> <i><a href =\"http://moigioinam.somee.com\">Click here to visit our website</a></i>"
            + "<br /> Thank for your Registing!";
        Basic.Replace send = new Basic.Replace();
        send.SendMail(emailAddress, content);
    }
         
}